'''
Created on Aug 13, 2012

@author: petrina
'''

from error.output_exceptions import CSV_exception
import csv
import logging


class CSV_export(object):
    '''
    Class for managing the csv-export (used by other classes such as Indicator).
    
    For exporting you need each line as a list and call write() for every line.
    The class uses csv (--> might not work properly on Windows systems).
    
    In order to export to a *.csv-File do the following:
        - init CSV_export
        - call open_writer()-method before you start writing data
        - call write for every row that shall be written to the csv-file
        - call close_writer() when you are done
    '''


    def __init__(self, delimiter = ',', path = '../output/',):
        '''
        
        @param delimiter: delimiter in the csv-file (default: ,)
        @param path: directory for the csv file (default: ../output/)
        @param filename: filename of the csv-file (default: file.csv)
        '''
        
        self.__path = path

        self.delimiter = delimiter
        logging.debug("CSV_export: created CSV_export")
        
        
    #-----------------------getter- and setter-methods-------------------------

    def get_delimiter(self):
        return self.__delimiter


    def set_delimiter(self, value):
        self.__delimiter = value


    def del_delimiter(self):
        del self.__delimiter


    #----------------------public methods of the class-------------------------

    def open_writer(self, filename):
        '''
        Opens a file to write and sets the writing parameters, such as delimiter.
        '''  
        
        try:
            logging.info("CSV_export: trying to open the writer at the path " + self.__path + filename)
            
            self._file = open(self.__path + filename, 'wb')
            self._writer = csv.writer(self._file, 
                                    delimiter=self.__delimiter, 
                                    quotechar='|', 
                                    quoting=csv.QUOTE_MINIMAL)
        except Exception, e:
            self.close_writer()
            raise CSV_exception("CSV_export: cannot open writer because: " + e.__str__())            
       
    def write(self, row):
        '''
        Writes the given row to the csv-file.
        
        @param row: a list containing data for the row
        '''
        
        logging.debug("CSV_export: writing given row to csv-file " + row.__str__())
        
        self._writer.writerow(row)


    def close_writer(self):
        '''
        Closes the csv-file-handle, i.e. writing is not possible anymore. 
        '''
        
        try:
            self._file.close()
            logging.debug("CSV_export: closed the writer")
        except:
            # obviously the writer was not open
            logging.error("CSV_export: cannot close the writer")
    
    #--------------assign properties using get and set-methods-----------------

    delimiter = property(get_delimiter, set_delimiter, del_delimiter, "delimiter's docstring")
        